import {RequestServer} from "./RequestServer.js";
import {ObtainLoginData} from "./ObtainLoginData";
import {ObtainNetWorkData} from "./ObtainNetWorkData";
import {ObtainSystemData} from "./ObtainSystemData";
import {ObtainUserData} from "./ObtainUserData";

export class DeterMineData extends RequestServer {
    constructor() {
        super();
        this.obtainUserData = new ObtainUserData();
        this.obtainLoginData = new ObtainLoginData();
        this.obtainSystemData = new ObtainSystemData();
        this.obtainNetWrokData = new ObtainNetWorkData();
    }

    deterMineData(failCallback, callbacks) {
        if (this.dataStore.get('userInfo') !== undefined &&
            this.dataStore.get('loginCode') !== undefined &&
            this.dataStore.get('systemInfo') !== undefined &&
            this.dataStore.get('networkType') !== undefined) {
            callbacks();
            return;
        }
        if (this.dataStore.get('userInfo') === undefined) {

            this.obainData(this.obtainUserData, failCallback, callbacks)

        }
        if (this.dataStore.get('loginCode') === undefined) {

            this.obainData(this.obtainLoginData, failCallback, callbacks)

        }
        if (this.dataStore.get('systemInfo') === undefined) {

            this.obainData(this.obtainSystemData, failCallback, callbacks)

        }
        if (this.dataStore.get('networkType') === undefined) {

            this.obainData(this.obtainNetWrokData, failCallback, callbacks)

        }
    }

    obainData(data, failCallback, callbacks) {
        data.getData(() => {
            this.conditionSuccess(callbacks);
        }, failCallback);
    }

    conditionSuccess(callbacks) {
        let s = true;
        if (this.dataStore.get('userInfo') === undefined ||
            this.dataStore.get('loginCode') === undefined ||
            this.dataStore.get('systemInfo') === undefined ||
            this.dataStore.get('networkType') === undefined) {
            s = false;
        }
        if (s) {
            callbacks();
        }
    }
}